package http.get;

import annotation.Path;
import annotation.Post;
import database.DBManager;
import bean.Bill;
import spark.Request;
import spark.Response;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

import static spark.Spark.get;
import static spark.Spark.post;

/**
 * Created by LeoLu on 2017/3/2.
 */

/**
 * 上传订单的接口
 */
@Path("/g/uploadBill")
public class ReqUploadBill extends BaseRequest {

    @Post
    public Object callAnyRespone(Request request, Response response) {
        String payType =  request.queryParams(   "payType");
        String userId = request.queryParams("userId");
        String price = request.queryParams("spendMoney");
        String playTime = request.queryParams("playTime");
        String billTime = request.queryParams("billTime");



        Bill bill = new Bill();
        bill.setBillTime(Long.parseLong(billTime));
        bill.setPayType(payType);
        bill.setSpendMoney(price);
        bill.setUserId(userId);
        bill.setPlayTime(playTime);

        return insertData(bill);

    }

    /**
     * 写入数据
     * @param bill
     * @return
     */
    private String insertData(Bill bill){
        String sql_queryAppid = "select * from user where userid=\'" + bill.getUserId() + "\'" ;
        String sql = "insert into billdata (userId,payType,spendMoney,entertainmentTime,billTime) values(?,?,?,?,?)";
        PreparedStatement statement =  DBManager.getInstance().getPreStatement(sql_queryAppid);
        try {
            if (statement.executeQuery().next()){
                 statement.close();
              PreparedStatement ss =  DBManager.getInstance().getPreStatement(sql);
              ss.setString(1,bill.getUserId());
              ss.setString(2,bill.getPayType());
              ss.setString(3,bill.getSpendMoney());
              ss.setString(4,bill.getPlayTime());
              ss.setString(5,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(bill.getBillTime()));
              ss.executeUpdate();
              return successCommanResp();
            } else {
                //TODO 无效的appId
                System.out.println("无效的APPID");
                return universalResp("无效的APPid","500");
            }
        }catch (Exception e){
            System.out.println("exception");
            e.printStackTrace();
        }
        return universalResp("数据错误","500");
    }



}
